package com.dataaccess.dataaccess.web.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import com.dataaccess.dataaccess.common.base.BaseService;
import com.dataaccess.dataaccess.support.model.RespEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author: YangYu
 * @ClassName: SatwellWirelessGatewayService
 * @Date: Created in 15:15 2021/12/16
 * @Description: 赛特威尔网关服务
 */
@Service
@Slf4j
public class SatwellWirelessGatewayService extends BaseService {

    @Resource
    private AsyncService asyncService;

    /**
     * @Author YangYu
     * @Description GSM03PRO（网关）
     * @Date Created in 15:16 2021/12/16
     * @Param [jsonParam]
     */

    public RespEntity GSM03PRO(JSONObject jsonParam) throws Exception {

        //原始数据
        String code  = jsonParam.getStr("code");
        //网关id
        String gatewayId = code.substring(6,12);
        //子设备类型
        String subDeviceType = code.substring(16,18);
        //命令
        String order = code.substring(22,24);
        //子设备编号
        String subDeviceNum = code.substring(26,32);
        //报警类型
        String alarmType = "";
        //报警内容
        String content = "";

        //烟雾(0x04)、燃气(0x05)、温感(0x0C)、CO(0x09)
        if (subDeviceType.equals("04") || subDeviceType.equals("05") || subDeviceType.equals("0c") || subDeviceType.equals("0c") || subDeviceType.equals("09")){
            if (order.equals("04")){
                alarmType = "火警";
                if (subDeviceType.equals("04")){
                    content = "发生烟雾警情";
                }else if (subDeviceType.equals("05")){
                    content = "发生燃气警情";
                }else if (subDeviceType.equals("0c")){
                    content = "发生温感警情";
                }else if (subDeviceType.equals("09")){
                    content = "发生CO警情";
                }
            }else if (order.equals("05")) {
                alarmType = "系统事件";
                content = "电池电压低于设定值";
            }else if (order.equals("07")) {
                alarmType = "系统事件";
                content = "手动按了测试按键、解除报警";
            }else if (order.equals("08")) {
                alarmType = "故障";
                content = "设备故障";
            }
        }else if (subDeviceType.equals("01")){
            if (order.equals("04")){
                alarmType = "火警";
                content = "按下手报按钮发出的信号";
            }else if (order.equals("05")) {
                alarmType = "系统事件";
                content = "电池电压低于设定值";
            }else if (order.equals("0a")) {
                alarmType = "系统事件";
                content = "手报是开启状态";
            }else if (order.equals("0b")) {
                alarmType = "系统事件";
                content = "手报已经复位";
            }
        }else if (subDeviceType.equals("02")){
            if (order.equals("04")){
                alarmType = "火警";
                content = "门开启了5分钟以上";
            }else if (order.equals("05")) {
                alarmType = "系统事件";
                content = "电池电压低于设定值";
            }else if (order.equals("0a")) {
                alarmType = "系统事件";
                content = "门磁是开启状态";
            }else if (order.equals("0b")) {
                alarmType = "系统事件";
                content = "门磁已经复位";
            }
        }else if (subDeviceType.equals("06")){
            if (order.equals("05")) {
                alarmType = "系统事件";
                content = "电池电压低于设定值";
            }else if (order.equals("0a")) {
                alarmType = "系统事件";
                content = "有水状态";
            }else if (order.equals("0b")) {
                alarmType = "系统事件";
                content = "无水状态";
            }
        }
        //推送报警信息
        JSONObject jsonParamN = new JSONObject();
        jsonParamN.put("deviceClassify","5");
        jsonParamN.put("number",gatewayId+subDeviceNum);
        jsonParamN.put("interfaceType","1");
        jsonParamN.put("alarmtime", DateUtil.now());
        jsonParamN.put("alarmTypeName",alarmType);
        jsonParamN.put("content",content);
        //rest
        System.out.println(jsonParamN);
        fireFightingiot(jsonParamN,"communication","notificationAlarmHandel","alarmInfo");
        jsonParamN.put("state","2");
        //异步更新传输装置状态
        asyncService.updateDeviceCurrentState(jsonParamN);
        return RespEntity.success();
    }

}
